package com.google.sydym6.view.activities;

import com.google.sydym6.R;
import com.google.sydym6.controllers.ISettingsController;
import com.google.sydym6.controllers.SettingsController;
import com.google.sydym6.view.activities.interfaces.IConnectionWithDialogFragment;
import com.google.sydym6.view.fragments.LastfmLoginDialogFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SettingsActivity  extends FragmentActivity implements IConnectionWithDialogFragment {
	
	ISettingsController controller;
	public Button vkSignInOutButton;
	public Button lastfmSignInOutButton;
	DialogFragment lastfmDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.settings_layout);
	    
	    controller = new SettingsController(this);
	    lastfmDialog = new LastfmLoginDialogFragment();
	    
	    vkSignInOutButton = (Button) findViewById(R.id.vkSingInOutButton);
	    changeVkButton();
	    vkSignInOutButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				controller.changeVk();
			}
	    	
	    });
	    
	    lastfmSignInOutButton = (Button) findViewById(R.id.lastfmSingInOutButton);
	    changeLastfmButton();
	    lastfmSignInOutButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				controller.changeLastfmAccount(getSupportFragmentManager(), "Login dialog");
			}
		});
	}
	
	public void changeVkButton() {
		if(controller.hasVkAccount()) {
	    	vkSignInOutButton.setText("Sign Out");
	    } else {
	    	vkSignInOutButton.setText("Sign In");
	    }
	}
	
	public void changeLastfmButton() {
		if (controller.hasLastfmAccount()) {
			lastfmSignInOutButton.setText("Sign Out");
		} else {
			lastfmSignInOutButton.setText("Sign In");
		}
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		controller.checkOnActivityResult(requestCode, resultCode, data);
		vkSignInOutButton.setEnabled(true);
    }

	@Override
	public void onConnect() {
		changeLastfmButton();
		lastfmSignInOutButton.setEnabled(true);
	}
	
}
